﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dukou.Domain;

namespace Dukou.WebSiteDesigner.Domain
{
    public class Container : HibernateEntity<Int64>
    {
        public virtual string Path { get; set; }
        
        public virtual Page Page { get; set; }

        public virtual int Width { get; set; }

        public virtual int Height { get; set; }

        public virtual ContainerType Type { get; set; }

        public virtual Container Parent { get; set; }

        public virtual Container Left { get; set; }

        public virtual Container Right { get; set; }

        public virtual IList<Container> Children { get; set; }

        public virtual IList<Container> Items 
        {
            get
            {
                if (this.Children == null)
                {
                    return new List<Container>();
                }

                if (Left != null)
                {

                }

                return this.Children.Where(x => !x.Equals(Left) || !x.Equals(Right)).ToList();
            }
        }

        public static IList<Container> Parse(string leafPaths)
        {
            IList<Container> returnValue = new List<Container>();
            if (string.IsNullOrEmpty(leafPaths))
            {
                return returnValue;
            }

            string[] paths = leafPaths.Split(';');

            foreach (var leafPath in paths)
            {
                if (string.IsNullOrEmpty(leafPath))
                {
                    continue;
                }

                string[] containers = leafPath.Split('.');
                Container parent = null;
                foreach (var container in containers)
                {
                    bool isNew = false;
                    var lParentheses = container.IndexOf('(');
                    var rParentheses = container.IndexOf(')');
                    var wh = container.Substring(lParentheses + 1, rParentheses - lParentheses - 1).Split(',');
                    var path = (parent == null ? "" : (parent.Path + ".")) + container.Substring(0, lParentheses);

                    Container con = returnValue.Where(x=>x.Path.Equals(path, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
                    if (con == null)
                    {
                        con = new Container()
                        {
                            Path = path,
                            Height = int.Parse(wh[1]),
                            Width = int.Parse(wh[0]),
                            Parent = parent,
                            Children = new List<Container>(),
                        };
                        isNew = true;
                    }

                    if (isNew)
                    {
                        if (container.StartsWith("containers"))
                        {
                            con.Type = ContainerType.Normal;
                        }
                        else if (container.StartsWith("items"))
                        {
                            con.Type = ContainerType.Normal;
                            if (con.Parent != null)
                            {
                                con.Parent.Children.Add(con);
                            }
                        }
                        else if (container.StartsWith("L"))
                        {
                            con.Type = ContainerType.Left;
                            if (con.Parent != null)
                            {
                                con.Parent.Children.Add(con);
                                con.Parent.Left = con;
                            }
                        }
                        else if (container.StartsWith("R"))
                        {
                            con.Type = ContainerType.Right;
                            if (con.Parent != null)
                            {
                                con.Parent.Children.Add(con);
                                con.Parent.Right = con;
                            }
                        }
                        returnValue.Add(con);
                    }

                    parent = con;
                }
            }

            return returnValue;
        }

        public virtual string Html() 
        {
            var html = string.Empty;
            if (this.Type == ContainerType.Normal)
            {
                html = string.Format("<div class=\"dk-container{{0}}\" style=\"width:{0}px;height:{1}px\">!@container!</div>", this.Width, this.Height);
                if (this.Left != null)
                {
                    html = string.Format("<div class=\"dk-container{{0}} clearfix\" style=\"width:{0}px;height:{1}px\">!@container!</div>", this.Width, this.Height);
                }
            }
            else if (this.Type == ContainerType.Left)
            {
                html = string.Format("<div class=\"dk-container dk-left{{0}}\" style=\"width:{0}px;height:{1}px\">!@container!</div>", this.Width, this.Height);
                if (this.Left != null)
                {
                    html = string.Format("<div class=\"dk-container dk-left{{0}} clearfix\" style=\"width:{0}px;height:{1}px\">!@container!</div>", this.Width, this.Height);
                }
            }
            else if (this.Type == ContainerType.Right)
            {
                html = string.Format("<div class=\"dk-container dk-right{{0}}\" style=\"width:{0}px;height:{1}px\">!@container!</div>", this.Width, this.Height);
                if (this.Left != null)
                {
                    html = string.Format("<div class=\"dk-container dk-right{{0}} clearfix\" style=\"width:{0}px;height:{1}px\">!@container!</div>", this.Width, this.Height);
                }
            }

            if ((this.Items == null || this.Items.Count == 0) && this.Left == null)
            {
                html = string.Format(html, " dk-leaf");
            }
            else
            {
                html = string.Format(html, string.Empty);
            }

            StringBuilder strBuilder = new StringBuilder();
            if (this.Items != null && this.Items.Count > 0)
            {
                foreach (var item in this.Items)
                {
                    strBuilder.Append(item.Html());
                }
                html = html.Replace("!@container!", strBuilder.ToString());
            }
            else if (this.Left != null)
            {
                strBuilder.Append(this.Left.Html());
                strBuilder.Append(this.Right.Html());

                html = html.Replace("!@container!", strBuilder.ToString());
            }
            else 
            {
                html = html.Replace("!@container!", "@Html.Action(\"Test\")");
            }
            return html;
        }
    }
}
